package util;

import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import twitter4j.PagableResponseList;
import twitter4j.Twitter;
import twitter4j.TwitterFactory;
import twitter4j.User;

public class GetUsuariosTwitter {
	public static void main(String[] args) {
    	try {
    		List<String> usersList = getFollowers("UsuarioInicial");
    		FileOutputStream allUsers = new FileOutputStream("Usuarios.txt");
    		for (String userScreenName: usersList) {
    			allUsers.write((userScreenName + System.getProperty("line.separator")).getBytes());
    			List<String> followersFriens = getFollowers(userScreenName);
    			for (String user: followersFriens) {
    				allUsers.write((user + System.getProperty("line.separator")).getBytes());
    			}
    		}
        } catch (Exception ex) {
        	System.err.println(ex.getMessage());
        }
    }
	
	@SuppressWarnings("deprecation")
	private static List<String> getFollowers(String userScreenName) {
		List<String> usersList = new ArrayList<String>();
		try {
    		Twitter twitter = new TwitterFactory().getInstance();
    		long cursor = -1;
    		PagableResponseList<User> users;
    		do {
                users = twitter.getFollowersStatuses(userScreenName, cursor);
                for (User user : users) {
                	usersList.add(user.getScreenName());
                }
            } while((cursor = users.getNextCursor()) != 0);
        } catch (Exception ex) {
        	System.err.println(ex.getMessage());
        }
		return usersList;
	}
}